package com.yuan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.reggie.common.R;
import com.yuan.reggie.pojo.Employee;
import com.yuan.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @author yuanxingke
 * @date 2022/7/30 0:05
 */
@RestController
@Slf4j
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    public EmployeeService employeeService;

    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        String name = employee.getUsername();
        String password = employee.getPassword();

        //md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //根据姓名查询数据库
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name);
        Employee emp = employeeService.getOne(queryWrapper);

        if (emp == null) {
            return R.error("用户名不正确");
        }

        //比对密码
        if (!emp.getPassword().equals(password)) {
            return R.error("密码输入错误");
        }

        //查看员工的状态
        if (emp.getStatus() == 0) {
            return R.error("该员工已经被禁用");
        }

        //存入session 将用户id 存入session中
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {

        request.getSession().invalidate();

        return R.success("退出成功");

    }

    @PostMapping
    public R save(@RequestBody Employee employee, HttpSession session) {
        //设置 默认添加密码 123456 并且md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置创建时间 创建人 更新时间 更新人
//        employee.setCreateTime(LocalDateTime.now());
        //从session中取出当前id
//        Long uid = (Long) session.getAttribute("employee");
//        employee.setCreateUser(uid);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((uid));
        //调用service方法
        employeeService.save(employee);
        return R.success("新增员工成功");
    }


    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        //设置分页
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //设置条件查询
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //设置排序
        queryWrapper.orderByDesc("update_time");
        employeeService.page(employeePage, queryWrapper);

        return R.success(employeePage);
    }


    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session) {

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) session.getAttribute("employee"));

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
